@Typed package org.mbte.groovypp.examples.wordcount

import java.util.regex.Pattern

for (i in 0..<10) {
    def t1 = System.currentTimeMillis()
    def counts = new HashMap<String,Integer> ()
    def matcher = Pattern.compile(/\w+/).matcher("")
    new File("./20_newsgroups").recurseFileIterator().each{ f ->
      if (!f.isDirectory() && !f.path.contains(".svn")) {
      f.charSequence.eachMatch(matcher) {String w -> w = w.toLowerCase ()
          counts[w] = (counts[w] ?: 0) + 1
      } } }
    new File("counts-descreasing-groovy").withWriter { Writer out ->
      counts.sort { a, b -> b.value - a.value }.each { k, v -> out << "$k\t$v\n" } }
    new File("counts-alphabetical-groovy").withWriter { Writer out ->
      counts.sort { a, b -> b.key <=> a.key }.each { k, v -> out << "$k\t$v\n" } }
    println "Finished in ${System.currentTimeMillis() - t1} millis"
}
